from fastapi import FastAPI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langserve import add_routes

model = ChatOpenAI(model="ictrek/qwen7b:32k",
                   openai_api_key="ollama",
                   openai_api_base="http://10.2.4.31:11434/v1/")
lang = "英语"
text = "您好，有什么可以帮助您的？"
msg = [{
    "role": "system",
    "content": "你是一个翻译专家，请将以下的内容翻译为{lang}"
}, {
    "role": "user",
    "content": "{text}"
}]

prompt_template = ChatPromptTemplate.from_messages([
    ('system', '你是一个翻译专家，请将以下的内容翻译为{lang}'),
    ('user', '{text}')
])

parser = StrOutputParser()

chain = prompt_template | model | parser

print(chain.invoke({"lang": lang, "text": text}))

# 创建FastAPI应用
app = FastAPI(title="我的LangChain服务", version="V1.0", description="翻译服务")

# 添加路由
add_routes(app, chain, path="/chat")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)



